import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledFrame
import os
import sys
from pathlib import Path
# sys.path.append(os.getcwd())
sys.path.append(str(Path(__file__).parent))
import UI.sn_entry as entry
import UI.group_select as combo_group
import UI.table as custom_table
import UI.stationConfig as stationConfig
import UI.themeSelect as themeSelect
import UI.transfer as transfer
import threading
from Log.outputLog import Log as log


def updateMode(mode):
    if mode == 'Audit':
        print("----mode:",mode)
    else:
        print("====mode:",mode)

class UiDisplay:
    def __init__(self):
        self.dataGrid = None
        self.dataGrid1 = None

        self.cbo = None

        self.tableTree = None
        self.tableTree1 = None
        self.dataArray = []

        self.groupArray = []
        self.resultArray = []

        self.selectIndex = "1"

        config = stationConfig.stationConfig()
        print("config group num: ",config.GroupCount)
        self.groupArray  = []
        x = 0
        for x in range(int(config.GroupCount)):
            
            self.groupArray.append(str(0))
            self.resultArray.append(str(0))
            x = x + 1

    
    def select(self,index):
        print("current selected -->", index)

        self.selectIndex = str(index)

        

        self.resultArray[int(index)] = "0"

        # 清空已显示列表
        self.dataGrid.delete(*self.dataGrid.get_children())
        num = int(index)
        for id in range(0,len(self.groupArray)):
            self.groupArray[id] = "0"

        self.groupArray[int(index)] = "1"
        # 展示数据
        for item in self.dataArray:
            for key_name in item.keys():
                print(key_name)
                if str(index) == key_name:
                    # print("选对了")
                    transfer.showDatas(self.dataGrid,item[index],self.resultArray,index)



    def createUI(self,win):
       
        # 创建一个水平方向的 panedwindow，并添加到主窗口中，默认为水平方向
        p_window1 = tk.PanedWindow(win)
        p_window1.pack(fill=tk.BOTH, expand=1)
        # 创建左上角window
        p_window2 = tk.PanedWindow(orient=tk.VERTICAL,width=300,showhandle=False,sashrelief='sunken')
        p_window1.add(p_window2)
        # 在 p_window2 中添加stationName Label
        top_label =tk.Label(p_window2, text='产品信息\n\nLimits 1.0.0', bg='#7171C6', height=8,width=8,font=('宋体',15))
        p_window2.add(top_label)


        p_window4 = tk.PanedWindow(orient=tk.VERTICAL,sashrelief='sunken')
        p_window1.add(p_window4)

        # top_label4 =tk. Label(p_window4, text='sequence', bg='#7171C6', height=8,font=('宋体',15))
        # p_window4.add(top_label4)
        # p_window4.config(background ="red")
        group_frame =tk.Frame(p_window4, bg='green',height=100,width=200)
        record_frame =tk.Frame(p_window4, bg='#CC99FF',height=100,width=600)

        cboS = combo_group.GroupSelect(group_frame,select_listen=self.select)

        tableTree = custom_table.CustomTable(record_frame)
        # tableTree.place(x=0,y=0,width=1000,height=600)

        # tableTree1 = custom_table.CustomTable(record_frame)
        # tableTree1.place(x=0,y=0,width=1000,height=600)

        self.dataGrid = tableTree.dataGrid
        self.cbo = cboS.cbo
        print("*************")
        print(self.cbo)
        # self.dataGrid1 = tableTree.dataGrid

        themeSelect.ThemeSelect(p_window2,select_listen=updateMode)
        # 创建左下角window
        p_window3 = tk.PanedWindow(width=300,orient=tk.VERTICAL,sashrelief='sunken')
        p_window2.add(p_window3)

        self.window3_child(p_window3)

        p_window4.add(group_frame)
        p_window4.add(record_frame)

    def window3_child(self,master):
        config = stationConfig.stationConfig()
        entry.DataEntryForm(master,config.isAudit,"group",self.dataGrid,self.dataArray,self.groupArray,self.resultArray,self.selectIndex,self.cbo)

if __name__ == "__main__":
    # win = ttk.Window(themename="litera")
    log().LogInfo("begin test UI")
    config = stationConfig.stationConfig()
    stationName = config.stationName
    stationVersion = config.stationVersion
    limitsVersion = config.LimitsVersion
    
    win = ttk.Window()
    win.title(stationName+stationVersion+' / Limits version : '+limitsVersion)
    win.geometry('1250x500+30+20')
    
    ui = UiDisplay()
    ui.createUI(win)
    
    print(combo_group.GroupSelect.str_value)

    win.mainloop()